#include <iostream>
#include <unistd.h>
#include <string>
#include <vector>
#include <fstream>

#include "msqlclient.hpp"

using namespace std;

// 题目信息
struct topic
{
    char title[300]{0};
    char opinion[150]{0};
    char answer[5]{0};

    int tsize = 0;
    int osize = 0;
    int asize = 0;

    void display()
    {
        cout << title;
        cout << opinion;
    }
};

vector<topic> top;

void initdata()
{
    ifstream ifs("mg.txt");
    string str;
    int csize = 0;
    str.resize(1024);

    while (!ifs.eof())
    {
        str[csize++] = ifs.get();
        if (str[csize - 1] == 'E')
        {
            int flag = 0;
            topic curtopic;
            for (int i = 0; i < csize-1; i++)
            {
                if (flag == 0)
                {
                    curtopic.title[curtopic.tsize++] = str[i];
                    if (str[i + 1] == '$')
                    {
                        flag++;
                        i++;
                    }
                }
                else if (flag == 1)
                {
                    curtopic.opinion[curtopic.osize++] = str[i];
                    if (str[i + 1] == '%')
                    {
                        flag++;
                        i++;
                    }
                }
                else
                {
                    if (str[i] >= 'A' && str[i] <= 'D')
                    {
                        curtopic.answer[curtopic.asize++] = str[i];
                    }
                }
            }
            csize = 0;
            top.push_back(curtopic);
        }
    }
    cout << "加载的数据个数:" << top.size() << endl;
    ifs.close();
}

class mysqlinsert
{
public:
    mysqlinsert()
        : _host(host), _user(user), _passwd(passwd), _db(db), _port(port)
    {
        _my = mysql_init(nullptr);
    }
    bool Connect()
    {
        if (mysql_real_connect(_my, _host.c_str(), _user.c_str(), _passwd.c_str(), _db.c_str(), _port, nullptr, 0) == nullptr)
        {
            cerr << "加载失败" << endl;
            return false;
        }
        mysql_set_character_set(_my, "utf-8");
        cout << "连接成功" << endl;
        return true;
    }
    void Insert()
    {
        string mode("insert into inittext(title,opinion,answer) values('");
        for (auto &e : top)
        {
            mode += e.title;
            mode += "','";
            mode += e.opinion;
            mode += "','";
            mode += e.answer;
            mode += "');";

            int n = mysql_query(_my, mode.c_str());
            if (n == 0)
            {
                cout << "插入成功" << endl;
                cout << mode << endl;
            }
            else
            {
                cout << "插入失败" << endl;
                cout << mode << endl;
            }
            mode = "insert into inittext(title,opinion,answer) values('";
        }
    }

private:
    string _host;
    string _user;
    string _passwd;
    string _db;
    int _port;
    MYSQL *_my;
};

mysqlinsert sq;
